<script type="text/javascript">

//单击事件
function <%= vb.treeID%>Click(event, treeId, treeNode) {
    event.preventDefault();
//    var zTree  = $.fn.zTree.getZTreeObj(treeId);
//    zTree.selectNode(treeNode,false);
    var detail =$('#<%= vb.treeID%>-detail');
    //alert(JSON.stringify(treeNode.rec));
    detail.find('input').each(function(i,n){
        $(n).val(treeNode.rec[$(n).attr('name')]);
    });
    detail.show();
}
//保存属性
function <%= vb.treeID%>SaveNode() {
	var zTree  = $.fn.zTree.getZTreeObj("<%= vb.treeID%>")
    //var upNode={rec: zTree.getSelectedNodes()[0].rec};
    var upNode=zTree.getSelectedNodes()[0];

    var detail =$('#<%= vb.treeID%>-detail');
    detail.find('input').each(function(i,n){
        upNode.rec[$(n).attr('name')] = $(n).val();
    });

	if (upNode.rec.c_name.length == 0) {
		$(this).alertmsg('error','名称不能为空！');
		return;
	}

	if (!upNode) {
        $(this).alertmsg('error','未选中任何节点！');
        return;
	}

    upNode.name =upNode.rec.c_ucode + upNode.rec.c_name;
    //递交保存数据库
    $.ajax({
        type: "POST",
        url: '/admin/code/code_save',
        data: upNode.rec,
//        async: false,
        success: function (ret) {
            if(ret.data.id >0){
                $(this).alertmsg('info','保存成功！');
                upNode.id = upNode.rec.id = ret.data.id;
                //更新树节点
                zTree.updateNode(upNode);
            }
        }
    });

}

//删除前事件
function M_BeforeRemove(treeId, treeNode) {
   // var $log = $('#ztree-log')
   // $log.val('准备删除：'+ treeNode.name +'！\n'+ $log.val())

    return true;
}
//删除结束事件
function M_NodeRemove(event, treeId, treeNode) {
    //var $log = $('#ztree-log')
   // $log.val('删除成功！\n'+ $log.val())
    $.ajax({
        type: "POST",
        url: '/admin/code/code_del',
        data: treeNode.rec,
        async: false,
        success: function (ret) {
            $('#<%= vb.treeID%>-detail').hide();
        }
    });
}

</script>

<div class="bjui-pageContent" style="padding:5px">
    <div style="float:left; display:block; overflow:auto; width:300px; height:100%; border:solid 1px #CCC; line-height:21px; background:#fff">
        <ul id="<%= vb.treeID%>" class="ztree" data-toggle="ztree"
            data-options="{
                expandAll: false,
                addHoverDom: 'edit',
                removeHoverDom: 'edit',
                beforeRemove: 'M_BeforeRemove',
                onRemove:'M_NodeRemove',
                maxAddLevel: 5,
                onClick: '<%= vb.treeID%>Click'
            }"
        >
            <% vb.list.map(function(rec){  %>
                <li data-id="<%= rec.id%>" data-pid="<%= rec.c_pid%>" data-rec="<%= JSON.stringify(rec)%>" data-open="<%= (rec.id == vb.rootID) %>" ><%= rec.c_ucode + rec.c_name%></li>
            <% });%>
        </ul>
    </div>
    <div id="<%= vb.treeID%>-detail" style="display:none; margin-left:320px; width:400px; height:400px;">
        <div class="bs-example" data-content="详细信息">
            <div class="form-group">
                <label  class="control-label x85">ID：</label>
                <input type="text" class="form-control" name="id" readonly="readonly" size="25" placeholder="可以为空" />
            </div>
            <div class="form-group">
                <label  class="control-label x85">编码：</label>
                <input type="text" class="form-control" name="c_ucode"  size="25" placeholder="可以为空" />
            </div>
            <div class="form-group">
                <label class="control-label x85">名称：</label>
                <input type="text" class="form-control validate[required] required" name="c_name"  size="25" placeholder="名称" />
            </div>
            <div class="form-group">
                <label  class="control-label x85">说明：</label>
                <input type="text" class="form-control" name="c_desc"  size="25" placeholder="" />
            </div>
            <div class="form-group">
                <label  class="control-label x85">类型：</label>
                <input type="text" class="form-control" name="c_type"  size="25" placeholder="" />
            </div>
            <div class="form-group">
                <label  class="control-label x85">对象名：</label>
                <input type="text" class="form-control" name="c_object"  size="25" placeholder="" />
            </div>
            <div class="form-group">
                <label  class="control-label x85">其他：</label>
                <input type="text" class="form-control" name="c_other"  size="25" placeholder="" />
            </div>
            <div class="form-group">
                <label  class="control-label x85">图标：</label>
                <input type="text" class="form-control" name="c_faicon"  size="25" placeholder="" />
            </div>
            <div class="form-group">
                <label  class="control-label x85">备注：</label>
                <input type="text" class="form-control" name="c_memo"  size="25" placeholder="" />
            </div>
            <div class="form-group" style="padding-top:8px; border-top:1px #DDD solid;">
                <label class="control-label x85"></label>
                <button class="btn btn-green" onclick="<%= vb.treeID%>SaveNode();">保 存</button>
            </div>
        </div>
    </div>
</div>
<div class="bjui-pageFooter" >
    <ul>
        <li><button type="button" class="btn-close" data-icon="close">关闭</button></li>
    </ul>
</div>